package cn.sso.app.client.example.services.impls;

import cn.ifloat.brick.basic.common.exceptions.SystemBasicException;
import cn.ifloat.brick.basic.common.exceptions.SystemExceptionCode;
import cn.ifloat.brick.basic.common.results.PageRequester;
import cn.ifloat.brick.basic.common.results.PageResult;
import cn.ifloat.brick.basic.common.toolkit.beans.BeanUtils;
import cn.ifloat.brick.sprofile.mybatis.common.BasicService;
import cn.ifloat.brick.sprofile.mybatis.common.BasicServiceImpl;
import cn.sso.app.client.example.domain.Project;
import cn.sso.app.client.example.mappers.ProjectMapper;
import cn.sso.app.client.example.services.ProjectService;
import cn.sso.app.client.example.vo.ProjectVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author: float 2023/1/12 09:53
 */
@Service
public class ProjectServiceImpl extends BasicServiceImpl<ProjectMapper, Project> implements ProjectService {


    @Override
    public void exampleConsole() {
    }

    @Override
    public Wrapper<ProjectVo> builderByVo(ProjectVo vo) {
        LambdaQueryWrapper<ProjectVo> wrapper = Wrappers.lambdaQuery(vo);
        if (StringUtils.isNotEmpty(vo.getNameLike())) {
            wrapper.like(ProjectVo::getName, vo.getNameLike());
        }
        if (StringUtils.isNotEmpty(vo.getStartTime())) {
            wrapper.ge(ProjectVo::getCreateTime, vo.getStartTime());
        }
        if (StringUtils.isNotEmpty(vo.getEndTime())) {
            wrapper.le(ProjectVo::getCreateTime, vo.getEndTime());
        }
        wrapper.orderBy(true,vo.isCtimeOrderBy(),ProjectVo::getCreateTime);
        return wrapper;
    }

    @Override
    public Wrapper<Project> builder(Project bean) {
        return Wrappers.lambdaQuery(bean);
    }



    @Override
    public ProjectVo getById(Long id) {
        LambdaQueryWrapper<ProjectVo> wrapper = Wrappers.<ProjectVo>lambdaQuery().eq(ProjectVo::getId, id);
        List<ProjectVo> list = this.getBaseMapper().listVo(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<ProjectVo> listVo(Project bean) {
        Wrapper<Project> wrapper = builder(bean);
        return this.getBaseMapper().listVo(wrapper);
    }

    @Override
    public PageResult<ProjectVo> pagerVo(PageRequester<ProjectVo> requester) {
        Wrapper<ProjectVo> wrapper = builderByVo(requester.getCondition());
        IPage<ProjectVo> page = this.getBaseMapper().pagerVo(Page.of(requester.getCurrent(), requester.getSize()), wrapper);
        return new PageResult<ProjectVo>(page.getRecords(), page.getTotal(), page.getPages(), page.getCurrent(), page.getSize());
    }

    @Override
    @Transactional
    public Project saver(Project bean) {
        bean.setId(this.sequenceId());
        this.save(bean);
        return bean;
    }

    @Override
    @Transactional
    public boolean saverOrUpdater(Project bean) {
        if (bean.getId() == null) {
            this.saver(bean);
        }else{
            return this.updateById(bean);
        }
        return true;
    }

}
